package com.example.luckin.slice.provider;

import com.example.luckin.ResourceTable;
import com.example.luckin.slice.entity.Item;
import ohos.agp.components.*;
import ohos.app.Context;

import java.util.List;

public class ItemProvider extends BaseItemProvider {
    private Context context;

    public ItemProvider(Context context, List<Item> list) {
        this.context = context;
        this.list = list;
    }

    private List<Item> list;
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if(component == null){
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fraction_menu_detail,componentContainer,false);
            ItemHolder itemHolder = new ItemHolder();
            itemHolder.txtName = (Text)component.findComponentById(ResourceTable.Id_txt_detail_name);
            itemHolder.txtNick = (Text)component.findComponentById(ResourceTable.Id_txt_detail_nick);
            itemHolder.txtComment = (Text)component.findComponentById(ResourceTable.Id_txt_detail_comment);
            itemHolder.txtPrice = (Text)component.findComponentById(ResourceTable.Id_txt_detail_price);
            itemHolder.txtDiscount = (Text)component.findComponentById(ResourceTable.Id_txt_detail_discount);
            itemHolder.img = (Image)component.findComponentById(ResourceTable.Id_img_item);
            itemHolder.txtTitle = (Text)component.findComponentById(ResourceTable.Id_txt_detail_title);
            component.setTag(itemHolder);
        }
        ItemHolder itemHolder = (ItemHolder) component.getTag();
        itemHolder.txtName.setText(list.get(i).getName());
        itemHolder.txtNick.setText(list.get(i).getNick());
        itemHolder.txtComment.setText(list.get(i).getComment());
        itemHolder.txtPrice.setText(list.get(i).getPrice());
        if( list.get(i).getDiscount() == null || list.get(i).getDiscount().equals("")){
            itemHolder.txtDiscount.setVisibility(1);
        }else{
            itemHolder.txtDiscount.setVisibility(0);
            itemHolder.txtDiscount.setText(list.get(i).getDiscount());
        }
        itemHolder.img.setPixelMap(list.get(i).getImg());
        if( list.get(i).getCategory() == null || list.get(i).getCategory().equals("")){
                itemHolder.txtTitle.setVisibility(1);
        }else{
            if(i>0 && list.get(i).getCategory().equals(list.get(i-1).getCategory())){
                itemHolder.txtTitle.setVisibility(1);
            }
            else {
                itemHolder.txtTitle.setVisibility(0);
                itemHolder.txtTitle.setText(list.get(i).getCategory());
            }
        }
        return component;
    }


    private class ItemHolder {
        private Text txtName;
        private Text txtNick;
        private Text txtComment;
        private Text txtPrice;
        private Text txtDiscount;
        private Image img;
        private Text txtTitle;
    }
}
